package com.wyix;


import com.wyix.model.InvisibilityCloak;
import com.wyix.utils.ConstructorAccessor;
import com.wyix.utils.FieldModifier;
import com.wyix.utils.ReflectionUtils;

public class ReflectionDemo {
    public static void main(String[] args) throws Exception {
        // 1. 获取Class对象
        Class<?> cloakClazz = ReflectionUtils.getClazz("com.wyix.model.InvisibilityCloak");
        ReflectionUtils.printClassInfo(cloakClazz);

        // 2. 通过私有构造创建实例（假设InvisibilityCloak有私有构造）
        InvisibilityCloak cloak = (InvisibilityCloak) ConstructorAccessor.createInstance(cloakClazz);

        // 3. 操作私有字段
        String originalOwner = (String) FieldModifier.getFieldValue(cloak, "owner");
        System.out.println("原主人: " + originalOwner);
        FieldModifier.setFieldValue(cloak, "owner", "哈利·波特");
        System.out.println("修改后主人: " + FieldModifier.getFieldValue(cloak, "owner"));
    }
}